package com.lei.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.lei.entity.Product;
import com.lei.util.DBHelp;
import com.lei.util.RowMapper;

public class ProductDao {
	
private DBHelp<Product> db=new DBHelp<Product>();

	public List<Product> findAll(){
		String sql="select id,name,price,shangjia from t_product";
		return db.executeQueryForList(sql, new ProductRowMapper());
	}
	
	
	
	public void save(Product p){
		String sql="insert into t_product(id,name,price,shangjia) values(?,?,?,?)";
		db.executeSQL(sql,p.getId(),p.getName(),p.getPrice(),p.isShangjia());
		
	}
	
	

	private class ProductRowMapper implements RowMapper<Product>{
	
		public Product mapperRow(ResultSet rs) throws SQLException {
			Product p = new Product();
			p.setId(rs.getString("id"));
			p.setName(rs.getString("name"));
			p.setPrice(rs.getFloat("price"));
			p.setShangjia(rs.getBoolean("shangjia"));
			return p;
		}
		
	}
		

}
